#!/usr/bin/env groovy

if(args == null || args.size() != 4){
	println "Usage -> findReplace arg1 arg2 arg3 arg4";
    println "arg1 : The folder to lookup";
	println "arg2 : The find file type(by extension)";
	println "arg3 : The source string";
	println "arg4 : The target string";
	return -1;
}
def path = new File(args[0]);
path.eachFileRecurse(
  {file ->
      if (file.name.endsWith(args[1])) {
        fileText = file.text;
		if(fileText.contains(args[2])){
			println "Find the string '${args[2]}' in ${file.name}";
			backupFile = new File(file.path + ".bak");
			backupFile.write(fileText);
			fileText = fileText.replaceAll(args[2], args[3])
			file.write(fileText);
		}
        
      }
  }
)
